﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using AForge.Video.FFMPEG;

namespace MovieMaker
{
	public class MovieMaker
	{

		public void Start()
		{
			Console.WriteLine(String.Format("Antal: " + NumberOfImages().ToString()));
			var startDate = DateTime.Parse("12 Mar 2012");
			var endDate = DateTime.Parse("13 Aug 2012");

			CreateMovie(startDate, endDate);

			Console.ReadLine();
		}

		private int NumberOfImages()
		{
			var number = -1;

			using (var container = new Tomato2012Entities())
			{
				var query = from d in container.ImageSet
				            select d;

				number = query.Count();
			}

			return number;
		}

		private void CreateMovie(DateTime startDate, DateTime endDate)
		{
			int width = 320, height = 240;
			var framRate = 200;

			using (var container = new Tomato2012Entities())
			{
				var query = from d in container.ImageSet
							where d.Date >= startDate && d.Date <= endDate
							select d;

				// create instance of video writer
				using (var vFWriter = new VideoFileWriter())
				{
					var imageConverter = new ImageConverter();

						// create new video file
					vFWriter.Open("framRate_" + framRate + ".avi", width, height, framRate, VideoCodec.Raw);

					var imageEntities = query.ToList();
					foreach (var imageEntity in imageEntities)
					{
						var imageByteArray = imageEntity.Data;
						var bmp = imageConverter.ToBitmap(imageByteArray);
						var bmpReduced = imageConverter.ReduceBitmap(bmp, width, height);

						Console.WriteLine("Current image: " + imageEntity.Id);
						vFWriter.WriteVideoFrame(bmpReduced);
					}

					Console.WriteLine("Done");

					vFWriter.Close();
				}
			}

		}
	}
}
